<?php

/*
 * GSPM (GetSimple Package Manager)
 * Version: 0.1
 *
 * Developed by Ryan Stubbs
 *
 * Project Development & Issue Tracking: http://code.google.com/p/gs-pm
 * 
 * For support, feature requests or to contribute, please visit the thread in
 * the official GetSimple forum.
 */

$plugin_file = basename(__FILE__, '.php');
$gspm_tmp_path = GSDATAOTHERPATH.'gspm/';

register_plugin(
	$plugin_file,
	'GSPM',
	'0.1',
	'Ryan Stubbs',
	'http://www.ryanstubbs.co.tv/',
	'A package manager for GetSimple to make installing extensions to your GetSimple website quick, simple and convenient.',
	'plugins',
	'gspm'
);

add_action('plugins-sidebar', 'createSideMenu', array($plugin_file, 'GSPM'));

function gspm() {
	global $SITEURL, $gspm_tmp_path;

	if(isset($_POST['submit'])) {
		if($_FILES["pfile"]["type"] !== "application/zip" && $_FILES["pfile"]["type"] !== "application/x-zip-compressed") {
			echo '<h2>Error!</h2>
			<p>The file you uploaded was not in .zip format. All uploaded plugins must be of this file type.</p>
			<p><strong><a href="javascript:history.go(-1)">Go back and try again</a></strong></p>';
		} else {
			if(!file_exists($gspm_tmp_path)) {
				mkdir($gspm_tmp_path, 0777);
			}

			$unique = mktime();
			$plugin_tmp = $gspm_tmp_path.'/'.$unique.'.zip';
			$plugin_tmp_dir = $gspm_tmp_path.'/'.$unique.'/';

			move_uploaded_file($_FILES["pfile"]["tmp_name"], $plugin_tmp);

			$zip = new ZipArchive();
			$zip->open($plugin_tmp);
			$zip->extractTo(GSPLUGINPATH);
			$zip->close();

			unlink($plugin_tmp);

			echo '<h2>Plugin Installed Successfully!</h2>
			<p>That\'s right. The plugin has now been uploaded, extracted and functionality of it should now be enabled.</p>
			<p><a href="'.$_SERVER['HTTP_REFERER'].'">Install another</a></p>';
		}
	} else {
		echo '<h2>Install New Plugin</h2>
		<h3>From a ZIP archive:</h3>
		<form method="POST" enctype="multipart/form-data">
		<p><input type="file" name="pfile" /></p>
		<p><input type="submit" name="submit" value="Upload and Install" /></p>
		</form>';
	}
}

?>
